<?php
namespace Menory\Standard\Sms;

abstract class AbstractSMSCenter {

    protected $messagePool = [];
    private $cache;

    /**
     * @var InterfaceAuth
     */
    protected $auth;

    /**
     * SMSCenter constructor.
     * @param $auth
     */
    public function __construct(InterfaceAuth $auth) {

        $this->auth = $auth;

    }

    /**
     * 添加短信模板
     * @param $message
     */
    public function add($message) {

        $this->messagePool[] = $message;

    }

    /**
     * 生成验证码
     * @param $number
     * @return string
     */
    public function genCode($number) {

        $random = "";
        srand((double) microtime() * 1000000);
        for ($i = 0; $i < $number; $i++) {
            $random .= rand(1, 9);
        }

        return $random;

    }

    /**
     * 发送短信
     * @param $messageType
     * @param $phone
     * @return mixed
     */
    abstract public function sendMessage($messageType, $phone);

    /**
     * @param mixed $cache
     */
    public function setCache($cache) {

        $this->cache = $cache;

    }

    /**
     * @param $messageType
     * @param $phone
     * @param $code
     */
    protected function cache($messageType, $phone, $code) {

        $cacheKey = $this->genCacheKey($messageType, $phone);
        $this->cache->set($cacheKey, $code, 2 * 60);

    }


    private function genCacheKey($messageType, $phone) {

        return sprintf("sms_%s_%s", $messageType, $phone);

    }

}